package com.car.controller.insurance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.user.UserDao;
import com.car.entity.Insurance;
import com.car.entity.User;
import com.car.service.impl.insurance.InsuranceServiceImpl;

import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InsuranceController {

    @Autowired
    InsuranceServiceImpl insuranceServiceimpl;

    @Autowired
    UserDao userDao;


    @RequestMapping("/toInsuranceList")
    public ModelAndView toInsuranceList() {
        return new ModelAndView("insurance-list");
    }

    @ResponseBody
    @RequestMapping("/insuranceList")
    public Map insuranceList(String carIdCard, String carInsuranceStart, String carInsuranceEnd, String PageNum, String PageSize) {
        Integer pageNum = Integer.valueOf(PageNum);
        Integer pageSize = Integer.valueOf(PageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("carIdCard", carIdCard);
        map.put("carInsuranceStart", carInsuranceStart);
        map.put("carInsuranceEnd", carInsuranceEnd);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

        Page page = insuranceServiceimpl.selectLikeInsurance(map);
        map.put("page", page);
        map.put("totalPage", page.getTotal() / page.getSize() == 0 ?
                page.getTotal() / page.getSize() :
                (page.getTotal() / page.getSize() + 1)
        );
        return map;
    }

    @ResponseBody
    @RequestMapping("/toAddInsurance")
    public ModelAndView toAddInsurance(){
        return new ModelAndView("insurance-add");
    }

    @ResponseBody
    @RequestMapping("/addInsurance")
    public ResultMsg addInsurance(Insurance insurance,String userName){
        ResultMsg resultMsg = insuranceServiceimpl.addInsurance(insurance,userName);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/delInsurance")
    public ResultMsg delInsurance(Insurance insurance) {
        ResultMsg resultMsg = insuranceServiceimpl.delInsurance(insurance);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/updateInsurance")
    public ResultMsg updateInsurance(Insurance insurance,String userName) {
        ResultMsg resultMsg = insuranceServiceimpl.updateInsurance(insurance);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/toUpdateInsurance")
    public ModelAndView toUpdateInsurance(Insurance insurance) {
        ModelAndView mv = new ModelAndView();
        ResultMsg resultMsg = insuranceServiceimpl.selectById(insurance);
        mv.setViewName("insurance-edit");
        mv.addObject("result", resultMsg);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/getUserById")
    public Map getUserById(int userId){
        Map<String,Object> map = new HashMap<>();
        User user = userDao.getUserById(userId);
        map.put("user",user);
        return map;
    }

    @ResponseBody
    @RequestMapping("/getAllUser")
    public List getAllUser(){
        List userList = userDao.getAllUser();
        return userList;
    }














//    @RequestMapping("/getPage")
//    public Map queryInsurancePage(int pageNum, int pageSize, Insurance insurance) {
//        Map map = new HashMap();
//        map.put("pageSize", pageSize);
//        map.put("pageNum", pageNum);
//        map.put("insurance", insurance);
//
//        Page page = insuranceService.queryInsurancePage(map);
//        map.put("page", page);
//        map.put("totalPage", page.getPages());
//
//        return map;
//    }

    /*@RequestMapping("/toList")
    public ModelAndView toList() {
        return new ModelAndView("insurance-list");
    }

    @RequestMapping("/getList")
    public ModelAndView getList(String carIdCard) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("list", insuranceService.getLikeInsurance(carIdCard));
        return mv;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        return new ModelAndView("insurance-add");
    }

    @RequestMapping("/addInsurance")
    @ResponseBody
    public ResultMsg addInsurance(Insurance insurance){
        ResultMsg resultMsg = insuranceService.addInsurance(insurance);
        return resultMsg;
    }

    @RequestMapping("/edit")
    public ModelAndView edit() {
        return new ModelAndView("insurance-edit");
    }

    @RequestMapping("/del")
    public ModelAndView delInsurance(Insurance insurance){
        insuranceService.delInsurance(insurance);
        return new ModelAndView("insurance-list");
    }*/
}
